package edu.ssdut.revise.models;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class VisCUNode extends VisBaseNode {
    private final String pathString;
    private final List<VisBaseNode> children = new ArrayList<>();
    private String packageStmt;
    private final List<String> importStmts;

    public List<VisBaseNode> getChildren() {
        return children;
    }

    public VisCUNode(CUWithInfo cuWithInfo) {
        super(cuWithInfo.getCu());

        this.pathString = cuWithInfo.pathString;
        CompilationUnit cu = cuWithInfo.getCu();

        cu.getPackageDeclaration().ifPresent(p -> this.packageStmt = p.toString());
        this.importStmts = cu.getImports().stream().map(Node::toString).collect(Collectors.toList());
        //add all the types
        for (TypeDeclaration<?> child : cu.getTypes()) {
            if (child instanceof ClassOrInterfaceDeclaration) {
                //construct a class/interface node
                children.add(new VisClassOrInterfaceNode((ClassOrInterfaceDeclaration) child));
            } else if (child instanceof EnumDeclaration) {
                children.add(new VisEnumNode((EnumDeclaration) child));
            }
        }
    }

    @Override
    public String getTypeName() {
        return "CUNode";
    }

    public String getPathString() {
        return pathString;
    }

    public String getPackageStmt() {
        return packageStmt;
    }

    public List<String> getImportStmts() {
        return importStmts;
    }
}
